package com.liang.ui;

import com.liang.utils.EmailUtils;
import com.liang.utils.PropertiesUtils;
import com.liang.utils.TimeUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.Date;

/**
 * @ClassName Login
 * @Description TODO
 * 登录页面 登录邮箱
 * @Author liang
 * @Date 2019/7/5 11:47
 * @Version 1.0
 **/
public class LoginEmail {
    private JFrame jFrame = new JFrame("登录");
    private Container c = jFrame.getContentPane();
    private JLabel a1 = new JLabel("用户名");
    private JTextField username = new JTextField();
    private JLabel a2 = new JLabel("密   码");
    private JPasswordField password = new JPasswordField();
    private JLabel a3 = new JLabel("起");
    private JTextField start = new JTextField();
    private JLabel a4 = new JLabel("止");
    private JTextField end = new JTextField();
    private JButton okbtn = new JButton("确定");
    private JButton cancelbtn = new JButton("取消");

    public LoginEmail(){
        //设置窗体的位置及大小
        jFrame.setBounds(600, 200, 350, 450);
        //设置一层相当于桌布的东西
        c.setLayout(new BorderLayout());//布局管理器
        //设置按下右上角X号后关闭
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //初始化--往窗体里放其他控件
        init();
        //设置窗体可见
        jFrame.setVisible(true);
    }

    private void init() {
        /*标题部分--North*/
        JPanel titlePanel = new JPanel();
        titlePanel.setLayout(new FlowLayout());
        titlePanel.add(new JLabel("Email ..."));
        c.add(titlePanel, "North");
        /*输入部分--Center*/
        JPanel fieldPanel = new JPanel();
        fieldPanel.setLayout(null);
        a1.setBounds(50, 20, 50, 30);
        a2.setBounds(50, 60, 50, 30);
        a3.setBounds(50, 100, 50, 30);
        a4.setBounds(50, 140, 50, 30);
        fieldPanel.add(a1);
        fieldPanel.add(a2);
        fieldPanel.add(a3);
        fieldPanel.add(a4);
        username.setBounds(110, 20, 150, 30);
        password.setBounds(110, 60, 150, 30);
        start.setBounds(110, 100, 150, 30);
        end.setBounds(110, 140, 150, 30);
        //设置固定格式
        username.setText(PropertiesUtils.getValue("username")+"@servyou.com.cn");
        password.setText(PropertiesUtils.getValue("password"));

        fieldPanel.add(username);
        fieldPanel.add(password);
        fieldPanel.add(start);
        fieldPanel.add(end);
        //设置默认时间
        start.setText(TimeUtils.dateToString(new Date()));
        end.setText(TimeUtils.dateToString(new Date()));
        c.add(fieldPanel, "Center");

        /*按钮部分--South*/
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.add(okbtn);
        buttonPanel.add(cancelbtn);
        c.add(buttonPanel, "South");

        //监听按钮
        action();
    }

    private void action() {
        okbtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String usn = username.getText();
                String psw = String.valueOf(password.getPassword());


                if ((usn.isEmpty()) ||((usn.trim().length() == 0)) || (psw.isEmpty())) {// 判断用户名或密码是否为空
                    JOptionPane.showMessageDialog(c, "用户名或密码为空！", null, JOptionPane.WARNING_MESSAGE);
                    return;
                }
                //写入配置文件


                //日期转换成date型
                Date startDate ;
                Date endDate;
                try {
                    startDate = TimeUtils.stringToDate(start.getText());
                    endDate = TimeUtils.stringToDate(end.getText());
                } catch (ParseException e1) {
                    JOptionPane.showMessageDialog(c, "日期格式错误！(例:2012/12/12)", null, JOptionPane.WARNING_MESSAGE);
                    return;
                }

                 try {
                     EmailUtils emailUtils = new EmailUtils();
                     String[][] messages = emailUtils.readEmail(usn, psw, startDate, endDate);
                    new MailBoxFrame(messages);//跳转显示邮件内容
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(c, e1.getMessage(), null, JOptionPane.WARNING_MESSAGE);
                    return;
                }


            }
        });

        cancelbtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                username.setText("");
                password.setText("");
            }
        });
    }
}
